﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 持仓明细表
    /// </summary>
    public class ServiceT_PositionDetail_History : Benefit.Interface.ServerData.IT_PositionDetail_History
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_PositionDetail_History(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 查询结算后的持仓明细情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_PositionDetail_History> GetInfo(string pdate)
        {
            List<Models.ServerData.T_PositionDetail_History> tph = new List<Models.ServerData.T_PositionDetail_History>();
            Interface.ServerData.IT_Settle_History tsh = new ServerData.ServiceT_Settle_History(db);
            //T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                tph = GetPositionDetailHistory(settlementid, pdate);
            }
            return tph;
        }

        /// <summary>
        /// 查询某天最大结算编号的资金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_PositionDetail_History> GetPositionDetailHistory(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_PositionDetail_History> tsm = new System.Collections.Generic.List<Models.ServerData.T_PositionDetail_History>();
            string sql = "select * from T_PositionDetail_History where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                if (Convert.ToInt32(sdr["volume"]) > 0)
                {
                    Models.ServerData.T_PositionDetail_History s = new Models.ServerData.T_PositionDetail_History();
                    s.AccountId = Convert.ToInt32(sdr["accountid"]);
                    s.Brokerid = Convert.ToString(sdr["Brokerid"]);
                    s.CloseAmount = Convert.ToDouble(sdr["CloseAmount"]);
                    s.CloseProfitByDate = Convert.ToDouble(sdr["CloseProfitByDate"]);
                    s.CloseProfitByTrade = Convert.ToDouble(sdr["CloseProfitByTrade"]);
                    s.CloseVolume = Convert.ToInt32(sdr["CloseVolume"]);
                    s.CombinStrumentId = Convert.ToString(sdr["CombinStrumentId"]);
                    s.Commission = Convert.ToDouble(sdr["Commission"]);
                    s.DeliveryDate = Convert.ToString(sdr["DeliveryDate"]);
                    s.Direction = Convert.ToString(sdr["Direction"]);
                    s.ExchangeId = Convert.ToString(sdr["ExchangeId"]);
                    s.Exchmargin = Convert.ToDouble(sdr["Exchmargin"]);
                    s.FloatingProfit = Convert.ToDouble(sdr["FloatingProfit"]);
                    s.HedgeFlag = Convert.ToString(sdr["HedgeFlag"]);
                    s.Id = Convert.ToInt32(sdr["Id"]);
                    s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
                    s.Investorid = Convert.ToString(sdr["Investorid"]);
                    s.Margin = Convert.ToDouble(sdr["Margin"]);
                    s.MarginRateByMoney = Convert.ToDouble(sdr["MarginRateByMoney"]);
                    s.MarginRateByVolume = Convert.ToDouble(sdr["MarginRateByVolume"]);
                    s.OpenDate = Convert.ToInt32(sdr["OpenDate"]);
                    s.OpenPrice = Convert.ToDouble(sdr["OpenPrice"]);
                    s.OpenTime = Convert.ToInt32(sdr["OpenTime"]);
                    s.PositionProfit = Convert.ToDouble(sdr["PositionProfit"]);
                    s.PositionProfitByDate = Convert.ToDouble(sdr["PositionProfitByDate"]);
                    s.PositionProfitByTrade = Convert.ToDouble(sdr["PositionProfitByTrade"]);
                    s.ProductId = Convert.ToString(sdr["ProductId"]);
                    s.SettleMentId = Convert.ToInt32(sdr["SettleMentId"]);
                    s.SettleMentPrice = Convert.ToDouble(sdr["SettleMentPrice"]);
                    s.TradeId = Convert.ToString(sdr["TradeId"]);
                    s.TradeType = Convert.ToString(sdr["TradeType"]);
                    s.TradingDay = Convert.ToString(sdr["TradingDay"]);
                    s.UpdateDate = Convert.ToInt32(sdr["UpdateDate"]);
                    s.Volume = Convert.ToInt32(sdr["Volume"]);
                    s.YesterdaySettlement = Convert.ToDouble(sdr["YesterdaySettlement"]);
                    tsm.Add(s);
                }
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return tsm;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerPositionDetailHistoryDataIntoBenefit(string pdate)
        {
            Interface.ServerData.IT_PositionDetail_History h = new ServerData.ServiceT_PositionDetail_History(db);
           // Models.ServerData.T_PositionDetail_History h = new Models.ServerData.T_PositionDetail_History();
            List<Models.ServerData.T_PositionDetail_History> tsm = h.GetInfo(pdate);
            foreach (Models.ServerData.T_PositionDetail_History ha in tsm)
            {
                db.PositionDetailHistory.Add(ha);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 删除某日的所有持仓明细记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayPositionDetailHistoryFromBenefit(string pdate)
        {
            var historys = from t in db.PositionDetailHistory where t.TradingDay.Equals(pdate) select t;
            foreach (Models.ServerData.T_PositionDetail_History ha in historys)
            {
                db.PositionDetailHistory.Remove(ha);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 得到某日某合约的保证金
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="tradingDay"></param>
        /// <param name="instrumentid"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public double GetAccountInstrument(int accountId, string tradingDay, string instrumentid)
        {
            double allSum = 0;
            var query = db.PositionDetailHistory.Where(a => a.AccountId == accountId).Where(a => a.TradingDay.Equals(tradingDay)).Where(a => a.InstrumentId.Equals(instrumentid));
            foreach (Models.ServerData.T_PositionDetail_History h in query)
            {
                allSum += h.Margin;
            }
            return allSum;
        }


        /// <summary>
        /// 得到某日某品种的保证金
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="tradingDay"></param>
        /// <param name="instrumentid"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public double GetAccountProduct(int accountId, string tradingDay, string productId)
        {
            double allSum = 0;
            var query = db.PositionDetailHistory.Where(a => a.AccountId == accountId).Where(a => a.TradingDay.Equals(tradingDay)).Where(a => a.ProductId.Equals(productId));
            foreach (Models.ServerData.T_PositionDetail_History h in query)
            {
                allSum += h.Margin;
            }
            return allSum;
        }
    }
}